package com.dynamic.idea.quick.develop.plugin.utils;

import com.alibaba.fastjson.JSON;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.model.Build;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.Resource;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

public class ModulesUtils {

	public static void main(String[] args) {
//		String pomFile="F:/Users/Administrator/git/dynamic-parent/dynamic-springboot/pom.xml";
		String pomFile="D:\\Users\\liuzhenhuan\\IdeaProjects\\shendu-order-parent\\pom.xml";
//		Plugin plugin = getSpringbootStartMainPlugin(pomFile);
//		System.out.println(JSON.toJSONString(plugin));

//		String mainClassName = getSpringbootStartMainClassName(pomFile);
//		System.out.println(mainClassName);

//		Build build = getBuild(pomFile);
//		List<Resource> resources = build.getResources();
//		System.out.println(JSON.toJSONString(resources));

//		List<String> customResources = getCustomResources(pomFile);
//		System.out.println(JSON.toJSONString(customResources));

		Model mavenModel = getMavenModel(pomFile);
		System.out.println(JSON.toJSONString(mavenModel,true));

		// 获取name
	}

	/**
	 * 获取maven的model
	 * @param pomFile
	 * @return
	 */
	public static Model getMavenModel(String pomFile) {
		try {
			FileInputStream fis = new FileInputStream(new File(pomFile));
			MavenXpp3Reader reader = new MavenXpp3Reader();
			Model model = reader.read(fis);

			return model;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 获取maven的build
	 * @param pomFile
	 * @return
	 */
	public static Build getBuild(String pomFile) {
		try {
			Model model = getMavenModel(pomFile);
			return getBuild(model);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 获取maven的build
	 * @param model
	 * @return
	 */
	public static Build getBuild(Model model) {
		try {
			Build build = model.getBuild();
			return build;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 获取maven的插件列表
	 * @param pomFile
	 * @return
	 */
	public static List<Plugin> getPlugins(String pomFile) {
		try {
            Model mavenModel = getMavenModel(pomFile);
			return getPlugins(mavenModel);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 获取maven的插件列表
	 * @param pomFile
	 * @return
	 */
	public static List<Plugin> getPlugins(Model pomFile) {
		try {
			Build build = getBuild(pomFile);
			List<Plugin> plugins = build.getPlugins();
			return plugins;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 得到springboot的启动插件
	 * @param pomFile
	 */
	public static Plugin getSpringbootStartMainPlugin(String pomFile) {
        Model mavenModel = getMavenModel(pomFile);
        return getSpringbootStartMainPlugin(mavenModel);
	}
	/**
	 * 得到springboot的启动插件
	 * @param model
	 */
	public static Plugin getSpringbootStartMainPlugin(Model model) {
		List<Plugin> plugins = getPlugins(model);
		if (CollectionUtils.isEmpty(plugins)) {
			return null;
		}
		for (Plugin plugin : plugins) {
			if (!StringUtils.equals(StringUtils.trim(plugin.getGroupId()), "org.springframework.boot")) {
				continue;
			}
			if (!StringUtils.equals(StringUtils.trim(plugin.getArtifactId()), "spring-boot-maven-plugin")) {
				continue;
			}
			return plugin;
		}
		return null;
	}

	/**
	 * 是否是library
	 * @param pomFile
	 * @return
	 */
	public static boolean isLibrary(String pomFile) {
		Plugin plugin = getSpringbootStartMainPlugin(pomFile);
		if (plugin==null) {
			return true;
		}
		return false;
	}
	/**
	 * 是否是启动项目
	 * @param pomFile
	 * @return
	 */
	public static boolean isBinary(String pomFile) {
		Plugin plugin = getSpringbootStartMainPlugin(pomFile);
		if (plugin==null) {
			return false;
		}
		return true;
	}

	/**
	 * TODO 目前只是强制是否存在资源文件src/main/proto的声明
	 * 后期会增加个proto文件的列表判断 by liuzhenhuan 20210924 add
	 * 判断是否存在protobuf
	 * @param pomFile
	 * @return
	 */
	public static boolean isContainProtobuf(String pomFile) {
		String resourceRoot="src/main/proto";
		List<Resource> resources = getResources(pomFile);
		if (CollectionUtils.isNotEmpty(resources)) {
			for (Resource resource : resources) {
				if (StringUtils.equals(resourceRoot, resource.getDirectory())) {
					return true;
				}
			}
		}
		return false;
	}



    /**
     *
     * @param model
     * @return
     */
	public static String getPacking(Model model){
	   if (model==null){
	       return null;
       }
	   return model.getPackaging();
    }

    /**
     *
     * @return
     */
	public static String getPacking(String pomFile){
	    Model model = getMavenModel(pomFile);
	    return getPacking(model);
    }

	/**
	 * 获取maven的插件列表
	 * @param pomFile
	 * @return
	 */
	public static List<Resource> getResources(String pomFile) {
		try {
            Model mavenModel = getMavenModel(pomFile);
            return getResources(mavenModel);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 获取maven的插件列表
	 * @param model
	 * @return
	 */
	public static List<Resource> getResources(Model model) {
		try {
			Build build = getBuild(model);
			List<Resource> resources = build.getResources();
			return resources;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


	/**
	 * TODO 暂时返回统一的src/main/java/下的文件
	 * 得到Java的资源文件
	 * @param pomFile
	 * @return
	 */
	public  static List<String> getJavaResources(String pomFile){
		String javaResourceRoot="src/main/java";
		List<String> resourceList=new ArrayList<String>();

		resourceList.add(javaResourceRoot+"/**/*.java");
		return resourceList;
	}

	/**
	 * TODO 暂时返回统一的src/main/resources/下的文件
	 * 得到Java的资源文件
	 * @param pomFile
	 * @return
	 */
	public static List<String> getCustomResources(String pomFile){
		String resourceRoot="src/main/resources";

		List<String> resourceList=new ArrayList<String>();
		List<Resource> resources = getResources(pomFile);
		if (CollectionUtils.isNotEmpty(resources)) {
			for (Resource resource : resources) {
				if (StringUtils.equals(resourceRoot, resource.getDirectory())) {
					List<String> includes = resource.getIncludes();
					for (String include : includes) {
						resourceList.add(resourceRoot+"/"+include);
					}
				}
			}
		}
		if (CollectionUtils.isEmpty(resourceList)) {
			resourceList.add(resourceRoot+"/**");
		}
		return resourceList;
	}

}
